/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;


import bean.Login;
import interfaces.LoginInterface;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author VENANCIO
 */
public class LoginDao implements LoginInterface{
    
    private Connection con;
    private String sql;
    private Login candidato;
    private List<Login> listaCandidato;
    
    public LoginDao(){
        
    }

    @Override
    public void inserir(Login login) {
        sql = "INSERT INTO LOGIN(EMAIL, SENHA, TIPO)VALUES(?,?,?)";
        try {
            con = new Conexao().getConexao();
            
            PreparedStatement stmt = con.prepareStatement(sql);
            int count=1;
            stmt.setString(count++, login.getEmail());
            stmt.setString(count++, login.getSenha());
            stmt.setString(count++, login.getTipo());
            stmt.executeUpdate();
            
            stmt.close();
            
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(LoginDao.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(LoginDao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        
        
    }

    @Override
    public Login consultar(Login login) {
        
        Login loginRetorno = null;
        
        sql= "SELECT * FROM LOGIN WHERE EMAIL = '?'";
        try {
            con = new Conexao().getConexao();
            
            PreparedStatement stmt = con.prepareStatement(sql);
            
            int count = 1;
            stmt.setString(count++, login.getEmail());
            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
                loginRetorno.setEmail(rs.getString("EMAIL"));
                loginRetorno.setSenha(rs.getString("SENHA"));
                loginRetorno.setTipo(rs.getString("TIPO"));
            }
            
            rs.close();
            stmt.close();
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(LoginDao.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(LoginDao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
       
        return loginRetorno;
       
    }

    @Override
    public void alterar(Login login) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void excluir(Login login) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
